package org.sky.axis2.security;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties.Authenticator;
import org.apache.axis2.wsdl.WSDLConstants;

public class SimpleAuthClient {
	private static EndpointReference targetEPR = new EndpointReference(
			"https://shnlap93:8443/Axis2Service/services/SimpleAuthService");
	private final static String usr = "Wright";
	private final static String pwd = "abcdefg";
	private final static String jksFile = "shnlap93.jks";
	private final static String jksFilePWD = "aaaaaa";

	private String getJskFilePath() {
		String filePath = "";
		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		URL url = classLoader.getResource(jksFile);
		if (url == null) {
			classLoader = ClassLoader.getSystemClassLoader();
			url = classLoader.getResource(jksFile);
		}
		filePath = url.getPath();
		System.out.println(filePath);
		return filePath;

	}

	public void getTax() {

		System.setProperty("javax.net.ssl.trustStore", getJskFilePath());// //
		System.setProperty("javax.net.ssl.trustStorePassword", jksFilePWD);
		ServiceClient sender = null;
		Authenticator authenticator = new Authenticator();
		List<String> auth = new ArrayList<String>();
		auth.add(Authenticator.BASIC);
		authenticator.setAuthSchemes(auth);
		authenticator.setUsername(usr);
		authenticator.setPassword(pwd);
		authenticator.setPreemptiveAuthentication(true);
		Options options = new Options();
		options.setTo(targetEPR);
		options.setAction("urn:getTax");
		options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);
		try {
			sender = new ServiceClient();
			sender.setOptions(options);
			OperationClient mepClient = sender
					.createClient(ServiceClient.ANON_OUT_IN_OP);
			MessageContext mc = new MessageContext();
			SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
			SOAPEnvelope env = fac.getDefaultEnvelope();
			OMNamespace omNs = fac.createOMNamespace(
					"http://security.axis2.sky.org", "");
			OMElement getTax = fac.createOMElement("getTax", omNs);
			OMElement salaryEle = fac.createOMElement("salary", omNs);
			salaryEle.setText("2100");
			getTax.addChild(salaryEle);
			env.getBody().addChild(getTax);
			mc.setEnvelope(env);
			mepClient.addMessageContext(mc);
			System.out.println("message====" + env);
			mepClient.execute(true);
			MessageContext response = mepClient
					.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
			SOAPBody body = response.getEnvelope().getBody();
			OMElement element = body.getFirstElement().getFirstChildWithName(
					new QName("http://security.axis2.sky.org", "return"));
			System.out.println(element.getText());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sender != null)
				try {
					sender.cleanup();
				} catch (Exception e) {
				}
		}

	}

	public static void main(String[] args) {
		SimpleAuthClient client = new SimpleAuthClient();
		client.getTax();
	}

}